// pages/order/order.js
const app = getApp();
Page({
  data: {
    api:app.globalData.api,
    mr:null,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    picker: ['喵喵喵', '汪汪汪', '哼唧哼唧'],
    multiArray: [
      ['无脊柱动物', '脊柱动物'],
      ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'],
      ['猪肉绦虫', '吸血虫']
    ],
    objectMultiArray: [
      [{
          id: 0,
          name: '无脊柱动物'
        },
        {
          id: 1,
          name: '脊柱动物'
        }
      ],
      [{
          id: 0,
          name: '扁性动物'
        },
        {
          id: 1,
          name: '线形动物'
        },
        {
          id: 2,
          name: '环节动物'
        },
        {
          id: 3,
          name: '软体动物'
        },
        {
          id: 3,
          name: '节肢动物'
        }
      ],
      [{
          id: 0,
          name: '猪肉绦虫'
        },
        {
          id: 1,
          name: '吸血虫'
        }
      ]
    ],
    multiIndex: [0, 0, 0],
    time: '9:00',
    date: '2018-12-25',
    region: ['广东省', '广州市', '海珠区'],
    imgList: [],
    imgList1: [],
    imgList2: [],
    modalName: null,
    textareaAValue: '',
    textareaBValue: ''
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  MultiChange(e) {

    this.setData({
      multiIndex: e.detail.value
    })
  },
  MultiColumnChange(e) {
    let data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'];
            data.multiArray[2] = ['猪肉绦虫', '吸血虫'];
            break;
          case 1:
            data.multiArray[1] = ['鱼', '两栖动物', '爬行动物'];
            data.multiArray[2] = ['鲫鱼', '带鱼'];
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
          case 0:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['猪肉绦虫', '吸血虫'];
                break;
              case 1:
                data.multiArray[2] = ['蛔虫'];
                break;
              case 2:
                data.multiArray[2] = ['蚂蚁', '蚂蟥'];
                break;
              case 3:
                data.multiArray[2] = ['河蚌', '蜗牛', '蛞蝓'];
                break;
              case 4:
                data.multiArray[2] = ['昆虫', '甲壳动物', '蛛形动物', '多足动物'];
                break;
            }
            break;
          case 1:
            switch (data.multiIndex[1]) {
              case 0:
                data.multiArray[2] = ['鲫鱼', '带鱼'];
                break;
              case 1:
                data.multiArray[2] = ['青蛙', '娃娃鱼'];
                break;
              case 2:
                data.multiArray[2] = ['蜥蜴', '龟', '壁虎'];
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        break;
    }
    this.setData(data);
  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  RegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },
  ChooseImage1() {
    let api = this.data.api
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList1.length != 0) {
          this.setData({
            imgList1: this.data.imgList1.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList1: res.tempFilePaths
          })
        }
        let that = this
        wx.uploadFile({
          filePath: this.data.imgList1[0],
          name: 'file',
          url: api+'/api/demo/Articles/imgupload',
          success:function(res){
            console.log(res.data)
            that.setData({
              img1:res.data
            })
          }
        })
      }

    });
  },
  ChooseImage2() {
    let api = this.data.api
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList2.length != 0) {
          this.setData({
            imgList2: this.data.imgList2.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList2: res.tempFilePaths
          })
        }
        let that = this
        wx.uploadFile({
          filePath: this.data.imgList2[0],
          name: 'file',
          url: api+'/api/demo/Articles/imgupload',
          success:function(res){
            that.setData({
              img2:res.data
            })
          }
        })



      }
    });
  },
  ChooseImage() {
    let api = this.data.api

    wx.chooseImage({
      count: 9, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        let that = this
        var img= []
        for(var i=0;i<this.data.imgList.length;i++){
          wx.uploadFile({
            filePath: this.data.imgList[i],
            name: 'file',
            url: api+'/api/demo/Articles/imgupload',
            success:function(res){
                img.push(res.data)
               that.data.img = img
            }
          })
        }


      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  ViewImage1(e) {
    wx.previewImage({
      urls: this.data.imgList1,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  onLoad: function (options) {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate()+1;
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    console.log(currentdate)
    this.setData({
      date:currentdate,
      now:currentdate
    })
    let id = options.did
    let api = this.data.api
    let that = this
    wx.request({
      url: api+'/api/demo/Articles/getOneDoctor?id='+id,
      success:function(res){
        that.setData({
          doctor:res.data.data
        })
      }
    })
    //获取缓存中的用户id
    wx.getStorage({
      key: 'uid',
      success:function(res){
        that.data.uid = res.data
        //获取默认患者信息
        wx.request({
          url: api+'/api/demo/Articles/gethuanzhe?uid='+res.data,
          success:function(res){
            that.setData({
              mr:res.data.data
            })
          }
        })
      }
    })
  },
  addhuanzhe:function(options){
    let id = options.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/add/add?id='+id,
    })
  },
  yuyue:function(options){
    if(options.detail.value.sid==''){
      wx.showToast({
        title: '请先填写患者信息',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(options.detail.value.desc==''){
      wx.showToast({
        title: '请填写病情描述',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    let api = this.data.api
    let that = this
    wx.getStorage({
      key: 'wenzhen',
      success:function(res){
        wx.request({
          url: api+'/api/demo/Articles/addorder',
          data: {
            'uid': that.data.uid,
            'a_content':JSON.stringify(res.data),
            'img1':that.data.img,
            'img2':that.data.img1,
            'img3':that.data.img2,
            'datas':JSON.stringify(options.detail.value)
          },
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res1) {
            if(res1.data){
              wx.showToast({
                title: '预约成功',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail:function(){
        wx.request({
          url: api+'/api/demo/Articles/addorder',
          data: {
            'uid': that.data.uid,
            'a_content':null,
            'img1':that.data.img,
            'img2':that.data.img1,
            'img3':that.data.img2,
            'datas':JSON.stringify(options.detail.value)
          },
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res1) {
            if(res1.data){
              wx.showToast({
                title: '预约成功',
                icon: 'none',
                duration: 2000,
                success:function(){
                  setTimeout(function () {
                    //要延时执行的代码
                   wx.reLaunch({
                     url: '/pages/personal/personal'
                   })
                  }, 1000) //延迟时间
                }
              })
              //清除问诊单缓存
              wx.removeStorage({
                key: 'a_content',
              })
            }
            
          }
        })
      }
    })
  
  },
  onPullDownRefresh(){
    this.onLoad();
    wx.stopPullDownRefresh()
  }
})